// #include "../common/net.hpp"
// #include "../common/dispatcher.hpp"
// #include "../server/rpcrouter.hpp"

/*  version1-------------------test------------*/

// void rpcRequestCallBack(const qiangsir::BaseConnection::ptr & conn, qiangsir::RpcRequest::ptr &msg)
// {
//     std::cout<<"收到rpc请求\n";
//     std::string body = msg->serialize();
//     std::cout<<body<<std::endl;
//     auto rpc_rsp = qiangsir:: BaseMessageFactory::create<qiangsir::RpcResponse>();
//     rpc_rsp->setId("1234567");
//     rpc_rsp->setType(qiangsir::MType::RSP_RPC);
//     rpc_rsp->setRcode(qiangsir::RCode::RCODE_OK);
//     rpc_rsp->setResult(33);
//     conn->send(rpc_rsp);
// }

// void topicRequestCallBack(const qiangsir::BaseConnection::ptr & conn, qiangsir::TopicRequest::ptr &msg)
// {
//     std::cout<<"收到topic请求\n";
//     std::string body = msg->serialize();
//     std::cout<<body<<std::endl;

//     auto topic_rsp = qiangsir:: BaseMessageFactory::create<qiangsir::TopicResponse>();
//     topic_rsp->setId("1234567");
//     topic_rsp->setType(qiangsir::MType::RSP_TOPIC);
//     topic_rsp->setRcode(qiangsir::RCode::RCODE_OK);

//     conn->send(topic_rsp);
// }
// int main()
// {
//     qiangsir::Dispatcher::ptr dispatcher = std::make_shared<qiangsir::Dispatcher>();

//     dispatcher->registerHandler<qiangsir::RpcRequest>(qiangsir::MType::REQ_RPC,rpcRequestCallBack);
//     dispatcher->registerHandler<qiangsir::TopicRequest>(qiangsir::MType::REQ_TOPIC,topicRequestCallBack);

//     auto message_cb = std::bind(&qiangsir::Dispatcher::onMessage,dispatcher.get(),
//     std::placeholders::_1,std::placeholders::_2);

//     qiangsir::BaseServer::ptr server = qiangsir::ServerFactory::create(9090);
//     server->setMessageCallBack(message_cb);
//     server->start();
// }


/* verson2-------test------*/

// void Add(const Json::Value &params, Json::Value &result)
// {
//     int num1 = params["num1"].asInt();
//     int num2 = params["num2"].asInt();

//     result = num1 + num2;
// }
// int main()
// {
//     qiangsir::Dispatcher::ptr dispatcher = std::make_shared<qiangsir::Dispatcher>();

//     qiangsir::server::RpcRouter::ptr router = std::make_shared<qiangsir::server::RpcRouter>();
//     std::unique_ptr<qiangsir::server::ServiceDescribeFactor> des_rpc(new qiangsir::server::ServiceDescribeFactor);
//     des_rpc->setMethodName("Add");
//     des_rpc->setParamsDesc("num1", qiangsir::server::param_type::INTEGRAL);
//     des_rpc->setParamsDesc("num2", qiangsir::server::param_type::INTEGRAL);
//     des_rpc->setReturnType(qiangsir::server::param_type::INTEGRAL);
//     des_rpc->setCallback(Add);
//     router->registerService(des_rpc->build());

//     auto rpcrequest_cb = std::bind(&qiangsir::server::RpcRouter::onRpcRequest, router.get(),
//                                    std::placeholders::_1, std::placeholders::_2);
//     dispatcher->registerHandler<qiangsir::RpcRequest>(qiangsir::MType::REQ_RPC, rpcrequest_cb);

//     qiangsir::BaseServer::ptr server = qiangsir::ServerFactory::create(9090);
//     auto message_cb = std::bind(&qiangsir::Dispatcher::onMessage,dispatcher.get(),
//                                  std::placeholders::_1, std::placeholders::_2);
//     server->setMessageCallBack(message_cb);
//     server->start();
// }

/* verson3-------test------*/
#include "../../server/server.hpp"
#include "../../common/detail.hpp"

void Add(const Json::Value &params, Json::Value &result)
{
    int num1 = params["num1"].asInt();
    int num2 = params["num2"].asInt();

    result = num1 + num2;
}
int main()
{
   std::unique_ptr<qiangsir::server::ServiceDescribeFactor> ser_desc(new qiangsir::server::ServiceDescribeFactor());
   ser_desc->setMethodName("Add");
   ser_desc->setParamsDesc("num1",qiangsir::server::param_type::INTEGRAL);
   ser_desc->setParamsDesc("num2",qiangsir::server::param_type::INTEGRAL);
   ser_desc->setCallback(Add);
   ser_desc->setReturnType(qiangsir::server::param_type::INTEGRAL);
   auto serivce1 = ser_desc->build();

   qiangsir::server::RpcServer server(qiangsir::Address("127.0.0.1",8888));
   server.registerService(serivce1);
   server.start();
}
